package com.lzh.o2o.service.impl;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lzh.o2o.dao.ShopDao;
import com.lzh.o2o.dto.ImageHolder;
import com.lzh.o2o.dto.ShopExecution;
import com.lzh.o2o.enums.ShopStateEnum;
import com.lzh.o2o.exception.ShopOperationException;
import com.lzh.o2o.pojo.Shop;
import com.lzh.o2o.service.ShopService;
import com.lzh.o2o.util.ImageUtil;
import com.lzh.o2o.util.PageCalculator;
import com.lzh.o2o.util.PathUtil;

/** 
* @author  haohao: 
* @date 创建时间：2018年9月9日 下午3:09:33 
* @version 1.0 
* @parameter 
* @since  
* @return  
*/
@Service
public class ShopServiceImpl implements ShopService {
	@Autowired
	private ShopDao shopDao;
	@Override
	@Transactional
	public ShopExecution addShop(Shop shop, ImageHolder thumbnail) throws ShopOperationException{
		//空值判断
		if(shop==null){
			return new ShopExecution(ShopStateEnum.NULL_SHOP);
		}
		try{
			/**
			 * 分为三步：
			 * 添加店铺的信息
			 * 返回上传的的图片
			 * 将图片新更新的地址跟新到数据库
			 * 三步一部出错，都需要回滚
			 */
			//给店铺信息赋初始值，初始为0 审核中
			shop.setEnableStatus(0);
			//设置店铺创建时间戳
			shop.setCreateTime(new Date());
			//设置店铺更新时间戳方便以后查看
			shop.setLastEditTime(new Date());
			int effectedNum = shopDao.insertShop(shop);
			//如果没有插入成功，即是创建店铺失败，而且要抛出RunntimeException,因为只有RunntimeException的时候，事务才会中止，进行回滚
			//测试添加失败是否回回滚一定要是运行时异常，不然还是会添加到数据库，不会回滚.
			//throw new ShopOperationException("店铺创建失败");

				if(effectedNum <= 0){
				throw new ShopOperationException("店铺创建失败");
			}else{
				//添加成功
				if(thumbnail.getImage() !=null){
					//存储图片
					try{
						addShopImg(shop,thumbnail);
					}catch(Exception e){
						throw new ShopOperationException("addShopImg error"+e.getMessage());
					}
					//更新店铺的图片地址
					effectedNum = shopDao.updateShop(shop);
					if(effectedNum <= 0){
						throw new ShopOperationException("更新图片地址失败");

					}
				}
			}
		}catch(Exception e){
			throw new ShopOperationException("addShop error:"+e.getMessage());
		}
		
		return new ShopExecution(ShopStateEnum.CHECK,shop);
	}
	private void addShopImg(Shop shop, ImageHolder thumbnail) {
		//获取shop图片目录的相对值路径
		String dest = PathUtil.getShopImagePath(shop.getShopId());
		String shopImgAddr =ImageUtil.generateThumbnail(thumbnail, dest);
		shop.setShopImg(shopImgAddr);
	}
	
	//根据店铺Id查找店铺信息
	@Override
	public Shop getByShopId(long shopId) throws ShopOperationException {
		return shopDao.queryByShopId(shopId);
	}
	//根据回显的数据进行保存
	@Override
	public ShopExecution modifyShop(Shop shop, ImageHolder thumbnail)
			throws ShopOperationException {
		if(shop==null||shop.getShopId()==null){
			return new ShopExecution(ShopStateEnum.NULL_SHOP);
		}else{
			try{
				
			
			//1.判断是否需要处理图片
			if(thumbnail.getImage()!=null&&thumbnail.getImageName() !=null&&!"".equals(thumbnail.getImageName())){
				Shop tempShop = shopDao.queryByShopId(shop.getShopId());
				//如果之前有图片，即需要将之前的图片给删除，然后在重新赋值
				if(tempShop.getShopImg()!=null){
					ImageUtil.deleteFilePath(tempShop.getShopImg());
				}
				addShopImg(shop,thumbnail);
			}
			//2.更新店铺信息
			shop.setLastEditTime(new Date());
			int effectedNum = shopDao.updateShop(shop);
			if(effectedNum<=0){
				return new ShopExecution(ShopStateEnum.INNER_ERROE);
			}else{
				//将已经更新好的数据返回前台
				shop=shopDao.queryByShopId(shop.getShopId());
				return new ShopExecution(ShopStateEnum.SUCCESS,shop);
			}
			}catch(Exception e){
				throw new ShopOperationException("modifyShop error:"+e.getMessage());
			}
		}

	}
	@Override
	//获取商铺列表分页展示
	public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) throws ShopOperationException {
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//获取店铺列表数据
		List<Shop> shopList = shopDao.queryShopList(shopCondition, rowIndex, pageSize);
		//获取对应列表数据总数
		int count = shopDao.queryShopCount(shopCondition);
		ShopExecution se = new ShopExecution();
		if(shopList !=null){
			//如果查询到有数据，将查到的商铺列表数据传入
			se.setShopList(shopList);
			se.setCount(count);
		}else{
			//如果店铺为空，即要返回一个状态值内部错误
			se.setState(ShopStateEnum.INNER_ERROE.getState());
		}
		return se;
	}

}
